package ltd.followkba.comment;




import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;

import ltd.followkba.comment.bean.BSongInfo;
import ltd.followkba.comment.bean.BTaskRun;
import ltd.followkba.comment.bean.BUserInfo;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.followkba.comment.MKCC.APPC_UPLOAD_JPG1;
import static ltd.followkba.comment.MKCC.APPC_UPLOAD_JPG2;
import static ltd.followkba.comment.MKCC.APPC_UPLOAD_MP3A;
import static ltd.followkba.comment.MKCC.APPC_UPLOAD_MP3B;
import static ltd.followkba.comment.MKCC.APPC_WANT_RANK;


public class TransHaoChang extends KgHaoChang {
    @Override
    public String uploadPhotoFile(String remFile, String locPhotoFile, BTaskRun bTaskRun){
        String gsLocal_1 = "";
        String gsLocal_2 = "";
        if ("".equals(gsUserID)){
            return "error：没有设置用户ID";
        }
        int g4Local_1 = 0;
        if (!"".equals(remFile)) {
            locPhotoFile = gsWorkPath + String.format("%d.jpg", System.currentTimeMillis());
            do {
                if (MUTIL.GET_URL_TO_FILE(remFile, locPhotoFile) > 0) {
                    break;
                }
            } while (++g4Local_1 < 5);
        }
        String upPhotoFile = String.format("AM_%s_1%s.jpg", gsUserID, MUTIL.GET_RANDOM_NUMBER(16));
        g4Local_1 = 0;
        ResultNet resultTemp = null;
        do {
            resultTemp = WriteCommand(APPC_UPLOAD_JPG1, upPhotoFile, String.format("%d", MAPI.GET_FILE_LENGTH(locPhotoFile)));
            if (resultTemp.sucess) {
                TransToken transToken = GetTransactionId(resultTemp.nextJason);
//MDWORD(0xde);MSTRING(resultTemp.nextJason);MSTRING(transToken.transactionId);MSTRING(transToken.key[0]);MSTRING(transToken.token[0]);
                gsLocal_1 = qiniu_com_small(transToken.key[0], transToken.token[0], "image/jpeg", upPhotoFile, locPhotoFile, bTaskRun);
                if (!"".equals(gsLocal_1)) {
                    gsLocal_2 = "[{\"response\":" + gsLocal_1 + ",\"filename\":\"" + upPhotoFile + "\",\"mode\":\"form\"}]";
                    resultTemp = WriteCommand(APPC_UPLOAD_JPG2, gsLocal_2, transToken.transactionId);
                    // mHaoChang.MakeStdReturn(gsLocal_1, netResult);
                    if (resultTemp.nextJason != null) {
                        gsLocal_1 = MUTIL.GET_JSON_STRING(resultTemp.nextJason, "avatar");
//MSTRING(gsLocal_1);
                        gsLocal_1 = MUTIL.GET_JSON_STRING(gsLocal_1, "original");
                        //        MAPI.DELETE_FILE(locPhotoFile);
                        if (!"".equals(gsLocal_1)) {
                            return "ok";
                        }
                    }
                }
            }
        } while (++g4Local_1 < 5);
        return "error";
    }
    @Override
    public String copyMediaFile(int video, int number, BUserInfo copyUserInfo, BTaskRun bTaskRun, String intro) {
        if (copyUserInfo.songQty < 1) {
            return "error:用户没有歌曲";
        }
        ResultNet resultNet;
        int g4Local_1 = copyUserInfo.songQty - 1;
        if (number != -1) {
            g4Local_1 = number;
        }
        BSongInfo aSongInfo = GetSongInfo(copyUserInfo.songIDs.get(g4Local_1));
        if (aSongInfo == null){
            return "error:读取歌曲信息出错(" + copyUserInfo.nickName + ")";
        }
        if (aSongInfo.intro.length() < bTaskRun.mBAccount.UploadIntroWord){
            aSongInfo.intro = MKCS.GetBingSongIntro(true);
        }
        String gsLocal_1;
//MSTRING(aSongInfo.WorksName);MSTRING(aSongInfo.Intro);MSTRING(aSongInfo.AudioUrl);MSTRING(aSongInfo.IdS);
        bTaskRun.UploadResult = String.format("好唱复制 %s，歌曲%d(%s) %s", copyUserInfo.nickName, g4Local_1, aSongInfo.songName, aSongInfo.path);
        MSTRING(bTaskRun.UploadResult);
        JSONObject jsonObjs = new JSONObject();
        JSONObject tuningSetting = new JSONObject();
        JSONObject singSetting = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        try {
            tuningSetting.put("effectId", 39);
            tuningSetting.put("raise", 0);
            tuningSetting.put("adjustId", 46);
            tuningSetting.put("humanVolume", aSongInfo.humanVolume);
            tuningSetting.put("beatTimerOffset", 500);
            tuningSetting.put("beatVolume", aSongInfo.beatVolume);
            jsonObjs.put("tuningSetting", tuningSetting);

            singSetting.put("humanVolume", aSongInfo.humanVolume);
            singSetting.put("headset", 1);
            singSetting.put("monitorId", 1);
            singSetting.put("beatVolume", aSongInfo.beatVolume);
            jsonObjs.put("singSetting", singSetting);

            jsonObjs.put("atFriends", jsonArray);
            jsonObjs.put("topics", jsonArray);
            jsonObjs.put("isTamper", 0);
            jsonObjs.put("isUseDefaultPhoto", 0);
            jsonObjs.put("beatId", aSongInfo.beatId);
            jsonObjs.put("workVersion", "4.3.2");
            jsonObjs.put("WorksName",  aSongInfo.songName);
            jsonObjs.put("singCount", 1);
            jsonObjs.put("Intro", aSongInfo.intro);
            jsonObjs.put("chorusBeatId", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if ("0".equals(aSongInfo.isMV)){
            video = 0;
        }
        String gsLocal_2 = "error";
        g4Local_1 = 0;
        do {
            MSTRING("uploadAudioFile "+video);
            if (video == 0) {
                gsLocal_2 = uploadAudioFile(aSongInfo.path, jsonObjs.toString(), aSongInfo.duration, bTaskRun);
            }
            else {
                gsLocal_2 = uploadVideoFile(aSongInfo.path, jsonObjs.toString(), aSongInfo.duration, bTaskRun);
            }
            if ("ok".equals(gsLocal_2)){
                return "ok";
            }
            else {
                bTaskRun.UploadResult = "上传出错："+g4Local_1+" "+gsLocal_2;
            }
            MAPI.MSLEEP(20);
        } while (++g4Local_1 < 5);
        return gsLocal_2;
    }



    public String uploadAudioFile(String audioFileUrl, String setting, String duration, BTaskRun bTaskRun) {
        String gsLocal_1;
        String gsLocal_2;
        String gsLocal_3;
        String gsLocal_4;
        int g4Local_1;

        if ("".equals(gsUserID)) {
            return "error：没有设置用户ID";
        }
        String locAudioFile = gsWorkPath + "upload.m4a";
        if (MUTIL.GET_URL_TO_FILE(audioFileUrl, locAudioFile) < 1) {
            return "无法下载" + audioFileUrl;
        }
        g4Local_1 = (int) MAPI.GET_FILE_LENGTH(locAudioFile);
        gsLocal_1 = String.format("%d", System.currentTimeMillis());
        String upAudioFile = "AM11595" + gsLocal_1 + ".m4a";
        gsLocal_2 = CreateTransaction(gsLocal_1, setting, duration, locAudioFile, "", "");
        gsLocal_3 = "[{\"filename\":\"" + upAudioFile + "\",\"size\":\"" + g4Local_1 + "\"}]";
        ResultNet resultTemp = WriteCommand(APPC_UPLOAD_MP3A, gsLocal_3, gsLocal_2);
        gsLocal_2 = "";
        TransToken transToken = null;
//MLOG(1117777);
        if (resultTemp.sucess) {
            transToken = GetTransactionId(resultTemp.nextJason);
//MDWORD(0x66);MLOG(transToken.length);
            if (g4Local_1 > 4194304 && !"".equals(transToken.Authorization[0])) {               // 多块传输
                bTaskRun.UploadResult = "Audio only大文件 "+g4Local_1;
                gsLocal_2 = qiniu_com_large(transToken.Authorization[0], "audio/x-m4a", g4Local_1, locAudioFile, bTaskRun);
                if (!"".equals(gsLocal_2)) {
                    gsLocal_2 = "[{\"response\":[" + gsLocal_2 + "],\"filename\":\"" + upAudioFile + "\",\"mode\":\"block\"}]";
                }
            } else if (g4Local_1 <= 4194304 && !"".equals(transToken.token[0])) {               // 小文件传输
                bTaskRun.UploadResult = "Audio only小文件 "+g4Local_1;
                gsLocal_2 = qiniu_com_small(transToken.key[0], transToken.token[0], "audio/x-m4a", upAudioFile, locAudioFile, bTaskRun);
                if (!"".equals(gsLocal_2)) {
                    gsLocal_2 = "[{\"response\":" + gsLocal_2 + ",\"filename\":\"" + upAudioFile + "\",\"mode\":\"form\"}]";
                }
            }
        }
        else {
            bTaskRun.UploadResult = "APPC_UPLOAD_MP3A"  + resultTemp.message;
            return bTaskRun.UploadResult;
        }
        if (!"".equals(gsLocal_2)) {
            resultTemp = WriteCommand(APPC_UPLOAD_MP3B, gsLocal_2, transToken.transactionId);
            if (resultTemp.sucess) {
                gsLocal_3 = MUTIL.GET_JSON_STRING(resultTemp.nextJason, "song");
                gsLocal_4 = MUTIL.GET_JSON_STRING(gsLocal_3, "songId");
//                        gsLocal_5 = MUTIL.GET_JSON_STRING(gsLocal_3, "audio");
                if (!"".equals(gsLocal_4)) {
                    resultTemp = WriteCommand(APPC_WANT_RANK, gsLocal_4, "");
                    if (resultTemp.sucess){
                        return "ok";
                    }
                }
            }
            else {
                return "APPC_UPLOAD_MP3B "  + resultTemp.message;
            }
        }
        if (resultTemp.message.startsWith("error")){
            return resultTemp.message;
        }
        return "error" + resultTemp.message;
    }
    public String uploadVideoFile(String remAudioFile, String setting, String duration, BTaskRun bTaskRun) {
        String gsLocal_1;
        String gsLocal_2;
        String gsLocal_3;
        String gsLocal_4;
        int g4Local_1;
        int g4Local_2;
        bTaskRun.UploadResult = "uploadVideoFile " + gsUserID;
        if ("".equals(gsUserID)) {
            bTaskRun.UploadResult = "error：没有设置用户ID";
            return bTaskRun.UploadResult;
        }
        String locAudioFile = gsWorkPath + "upload.m4a";

        if (MUTIL.GET_URL_TO_FILE(remAudioFile, locAudioFile) < 1) {
            bTaskRun.UploadResult = "error：无法下载" + locAudioFile;
            return bTaskRun.UploadResult;
        }
        String remVideoFile = remAudioFile.replace(".m4a", ".mp4");
        String locVideoFile = gsWorkPath + "upload.mp4";
        if (MUTIL.GET_URL_TO_FILE(remVideoFile, locVideoFile) < 1) {
            bTaskRun.UploadResult = "error：无法下载" + remVideoFile;
            return bTaskRun.UploadResult;
        }

        g4Local_1 = (int) MAPI.GET_FILE_LENGTH(locAudioFile);
        g4Local_2 = (int) MAPI.GET_FILE_LENGTH(locVideoFile);
//         mHaoChang.mAuthorizeToken = mToken;
        gsLocal_1 = String.format("%d", System.currentTimeMillis());
        String upAudioFile = "AM11595" + gsLocal_1 + ".m4a";
        String upVideoFile = "AM11595" + gsLocal_1 + ".mp4";
        gsLocal_2 = CreateTransaction(gsLocal_1, setting, duration, locAudioFile, locVideoFile, "");
        gsLocal_3 = "[{\"filename\":\"" + upAudioFile + "\",\"size\":\"" + g4Local_1 + "\"}";
        gsLocal_3 += ",{\"filename\":\"" + upVideoFile + "\",\"size\":\"" +g4Local_2 + "\"}]";
//MLOG(gsLocal_3);MLOG(gsLocal_2);
        ResultNet resultTemp = WriteCommand(APPC_UPLOAD_MP3A, gsLocal_3, gsLocal_2);
        gsLocal_2 = "";
        gsLocal_3 = "";
        TransToken transToken = null;
        if (resultTemp.sucess) {
            transToken = GetTransactionId(resultTemp.nextJason);
//MDWORD(0x66);MLOG(transToken.length);
//MLOG(transToken.filename[0]); MLOG(transToken.token[0]);MLOG(transToken.key[0]);
//MLOG(transToken.filename[1]);MLOG(transToken.Authorization[1]);

            if (g4Local_1 > 4194304 && !"".equals(transToken.Authorization[0])) {               // 多块传输
                bTaskRun.UploadResult = "Audio大文件 "+g4Local_1;
                gsLocal_2 = qiniu_com_large(transToken.Authorization[0], "audio/x-m4a", g4Local_1, locAudioFile, bTaskRun);
                if (!"".equals(gsLocal_2)) {
                    gsLocal_2 = "[{\"response\":[" + gsLocal_2 + "],\"filename\":\"" + upAudioFile + "\",\"mode\":\"block\"},";
                }
            }
            else if (g4Local_1 <= 4194304 && !"".equals(transToken.token[0])) {               // 小文件传输
                bTaskRun.UploadResult = "Audio小文件 "+g4Local_1;
                gsLocal_2 = qiniu_com_small(transToken.key[0], transToken.token[0], "audio/x-m4a", upAudioFile, locAudioFile, bTaskRun);
                if (!"".equals(gsLocal_2)) {
                    gsLocal_2 = "[{\"response\":" + gsLocal_2 + ",\"filename\":\"" + upAudioFile + "\",\"mode\":\"form\"},";
                }
            }
            if (g4Local_2 > 4194304 && !"".equals(transToken.Authorization[1])) {               // 多块传输
                bTaskRun.UploadResult = "Video大文件 "+g4Local_2;
                gsLocal_3 = qiniu_com_large(transToken.Authorization[1], "video/x-mp4", g4Local_2, locVideoFile, bTaskRun);
                if (!"".equals(gsLocal_3)) {
                    gsLocal_3 = "{\"response\":[" + gsLocal_3 + "],\"filename\":\"" + upVideoFile + "\",\"mode\":\"block\"}]";
                }
            }
            else if (g4Local_2 <= 4194304 && !"".equals(transToken.token[1])) {               // 小文件传输
                bTaskRun.UploadResult = "Video小文件 "+g4Local_2;
                gsLocal_3 = qiniu_com_small(transToken.key[1], transToken.token[1], "video/x-mp4", upVideoFile, locVideoFile, bTaskRun);
                if (!"".equals(gsLocal_3)) {
                    gsLocal_3 = "{\"response\":" + gsLocal_3 + ",\"filename\":\"" + upVideoFile + "\",\"mode\":\"form\"}]";
                }
            }
        }
        else {
            bTaskRun.UploadResult = "APPC_UPLOAD_MP3A"  + resultTemp.message;
            return bTaskRun.UploadResult;
        }

//MLOG(gsLocal_2+gsLocal_3);
        if (!"".equals(gsLocal_2) && !"".equals(gsLocal_3)) {
            resultTemp = WriteCommand(APPC_UPLOAD_MP3B, gsLocal_2 + gsLocal_3, transToken.transactionId);
            if (resultTemp.sucess) {
                gsLocal_3 = MUTIL.GET_JSON_STRING(resultTemp.nextJason, "song");
                gsLocal_4 = MUTIL.GET_JSON_STRING(gsLocal_3, "songId");
//                        gsLocal_5 = MUTIL.GET_JSON_STRING(gsLocal_3, "audio");
                if (!"".equals(gsLocal_4)) {
                    resultTemp = WriteCommand(APPC_WANT_RANK, gsLocal_4, "");
                    if (resultTemp.sucess){
                        return resultTemp.message;
                    }
                    else {
                        return "APPC_WANT_RANK"  + resultTemp.message;
                    }
                }
            }
            else {
                return "APPC_UPLOAD_MP3A"  + resultTemp.message;
            }
        }
        return "error" + resultTemp.message;
    }


    public TransToken GetTransactionId(String returnJson){
        TransToken transToken = new TransToken();
        transToken.transactionId = MUTIL.GET_JSON_STRING(returnJson, "transactionId");
        String gsLocal_1 = MUTIL.GET_JSON_STRING(returnJson, "files");
        JSONArray jsonArray;
// MLOG(gsLocal_1);
        try {
            jsonArray = new JSONArray(gsLocal_1);
            int g4Local_1 = jsonArray.length();
            if (g4Local_1 > 0) {
                transToken.length = g4Local_1;
                transToken.filename = new String[g4Local_1];
                transToken.token = new String[g4Local_1];
                transToken.key = new String[g4Local_1];
                transToken.Authorization = new String[g4Local_1];
                int g4Local_2 = 0;
                String gsLocal_2;
                String gsLocal_3;
                while (g4Local_2 < g4Local_1) {
                    JSONObject jsonObj = jsonArray.getJSONObject(g4Local_2);
                    transToken.filename[g4Local_2] = jsonObj.getString("filename");
                    gsLocal_2 = jsonObj.getString("mode");
//MLOG(gsLocal_2);
                    if ("form".equals(gsLocal_2)){
                        gsLocal_3 = jsonObj.getString("parameters");
                        transToken.token[g4Local_2] = MUTIL.GET_JSON_STRING(gsLocal_3, "token");
                        transToken.key[g4Local_2] = MUTIL.GET_JSON_STRING(gsLocal_3, "key");
                        transToken.Authorization[g4Local_2] = "";
                    }
                    else {
                        transToken.token[g4Local_2] = "";
                        transToken.key[g4Local_2] = "";
                        gsLocal_3 = jsonObj.getString("blocks");
                        JSONArray bkArray = new JSONArray(gsLocal_3);
                        if (jsonArray.length() > 0) {
                            JSONObject bkObject = bkArray.getJSONObject(0);
                            gsLocal_2 = bkObject.getString("headers");
                            transToken.Authorization[g4Local_2] = MUTIL.GET_JSON_STRING(gsLocal_2, "Authorization");
                        }
                    }
                    ++g4Local_2;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return transToken;
    }

    public String CreateTransaction(String currentTime, String setting, String duration, String locAudioFile, String locVideoFile, String locPhotoFile) {
        String upAudioFile = "AM11595" + currentTime + ".m4a";
        String upVideoFile = "AM11595" + currentTime + ".mp4";
         try {
            JSONObject jsonObjs = new JSONObject(setting);
            JSONArray jsonArray = new JSONArray();
            JSONObject audioJson = new JSONObject();
            JSONObject videoJson = new JSONObject();
            JSONObject filesJson = new JSONObject();
            if (!"".equals(locAudioFile)) {
                 audioJson.put("md5", MUTIL.GET_FILE_MD5(locAudioFile));
                 audioJson.put("duration", duration);
                 audioJson.put("file", upAudioFile);
                 audioJson.put("size", MAPI.GET_FILE_LENGTH(locAudioFile));
                 filesJson.put("audio", audioJson);
            }
             if (!"".equals(locVideoFile)) {
                 videoJson.put("md5", MUTIL.GET_FILE_MD5(locVideoFile));
                 videoJson.put("duration", duration);
                 videoJson.put("file", upVideoFile);
                 videoJson.put("size", MAPI.GET_FILE_LENGTH(locVideoFile));
                 filesJson.put("video", videoJson);
             }
            filesJson.put("photos", jsonArray);
            jsonObjs.put("files", filesJson);
            return jsonObjs.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }
    public String qiniu_com_small(String key, String token, String fileType, String upFile, String locFile, BTaskRun bTaskRun){
        OkHttpClient mOkHttpClient = new OkHttpClient();
        File file = new File(locFile);
        String gsLocal_1 = "";
        Response response = null;
        returnBusyMessage(70, 0, locFile);
        try {
            RequestBody fileBody = RequestBody.create(MediaType.parse(fileType), file);
            RequestBody requestBody = new MultipartBuilder()
                    .type(MultipartBuilder.FORM)
                    .addPart(Headers.of("Content-Disposition", "form-data; name=\"token\""), RequestBody.create(null, token))
                    .addPart(Headers.of("Content-Disposition", "form-data; name=\"key\""), RequestBody.create(null, key))
                    .addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"" + upFile + "\""), fileBody)
                    .build();
            Request request = new Request.Builder()
                    .url("http://upload.qiniu.com/")
                    .header("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.4)")
                    .post(requestBody)
                    .build();
            returnBusyMessage(70, 20, "");
            response = mOkHttpClient.newCall(request).execute();
            returnBusyMessage(70, 70, "");
            gsLocal_1 = response.body().string();
            if (response.code() < 200 || response.code() > 201) {
                return "";
            }
            Map<String, List<String>> map = response.headers().toMultimap();
            JSONObject jsonObj = new JSONObject();
            int g4Local_1;
            String gsLocal_2;
            try {
//MLOG(response.headers().toString());MDWORD(0xee);
                for (String mapkey : map.keySet()) {
                    gsLocal_2 = map.get(mapkey).toString().replace("[", "").replace("]", "");
                    if (mapkey.startsWith("OkHttp")){
                    }
                    else if ("X-Reqid".equals(mapkey)){
                        g4Local_1 = gsLocal_2.indexOf(",");
                        if (g4Local_1 > 0) {
                            gsLocal_2 = gsLocal_2.substring(0, g4Local_1);
                        }
                        jsonObj.put(mapkey, gsLocal_2);
                    }
                    else {
                        jsonObj.put(mapkey, gsLocal_2);
                    }
                }
                JSONObject jsonObjs = new JSONObject();
                jsonObjs.put("headers", jsonObj);
                jsonObjs.put("body", gsLocal_1);
                jsonObjs.put("code", response.code());
                gsLocal_1 = jsonObjs.toString();
            } catch (JSONException e) {
            }
            return gsLocal_1;
        } catch (IOException e) {
            gsLocal_1 = e.toString();
        }
        return gsLocal_1;
    }
    public String qiniu_com_large(String Authorization, String fileType, int length, String localFile, BTaskRun bTaskRun) {
        RandomAccessFile gfLocal_1 = null;
        String gsLocal_1 = "";
        String gsLocal_2;
        String gsLocal_3;
//MDWORD(0xee);MLOG(length/4194304);
        try {
            gfLocal_1 = new RandomAccessFile(localFile, "r");
            byte[] gpLocal_1 = new byte[4194304];
            int g4Local_1 = length;
            int g4Local_2 = 0;
            int g4Local_3;
            int g4Local_4 = 0;
            do {
                if (g4Local_1 > 4194304) {
                    g4Local_3 = 4194304;
                    g4Local_1 -= 4194304;
                } else {
                    g4Local_3 = g4Local_1;
                    g4Local_1 = 0;
                }
//    MLOG(g4Local_1);MLOG(g4Local_3);
                bTaskRun.UploadResult = "正在上传 " + MUTIL.GET_PERCENT_COUNT(g4Local_4, length) + " %";
                gfLocal_1.read(gpLocal_1, 0, g4Local_3);
                gsLocal_2 = String.format("http://upload.qiniu.com/mkblk/%d", g4Local_3);
//MLOG(uploadUrl);MLOG(Authorization);
//MLOG(g4Local_2);
                gsLocal_3 = qiniu_com_block(gsLocal_2, Authorization, fileType, g4Local_2, g4Local_3, gpLocal_1, bTaskRun);
                if (g4Local_1 > 0) {
                    gsLocal_1 += gsLocal_3 + ",";
                } else {
                    gsLocal_1 += gsLocal_3;
                }
                ++g4Local_2;
                g4Local_4 += g4Local_3;
            } while (g4Local_1 > 0);
            gfLocal_1.close();
//MLOG(2222233);MLOG(gsLocal_1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return gsLocal_1;
    }
    public String qiniu_com_block(String uploadUrl, String Authorization, String fileType, int index, int length, byte[]in_data, BTaskRun bTaskRun) {
        OkHttpClient mOkHttpClient = new OkHttpClient();
         String gsLocal_1 = "";
        Response response = null;
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse(fileType),in_data, 0, length);
            Request request = new Request.Builder()
                    .url(uploadUrl)
                    .header("Authorization", Authorization)
                    .header("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.4)")
                    .post(requestBody)
                    .build();
            response = mOkHttpClient.newCall(request).execute();
            gsLocal_1 = response.body().string();
            if (response.code() < 200 || response.code() > 201) {
                return "";
            }
            String gsLocal_2;
            Map<String, List<String>> map = response.headers().toMultimap();
            JSONObject jsonObj = new JSONObject();
            try {
                for (String key : map.keySet()) {
                    gsLocal_2 = map.get(key).toString().replace("[", "").replace("]", "");
                    jsonObj.put(key, gsLocal_2);
                }
                JSONObject jsonObjs = new JSONObject();
                jsonObjs.put("headers", jsonObj);
                jsonObjs.put("blockIndex", index);
                jsonObjs.put("body", gsLocal_1);
                jsonObjs.put("code", response.code());
                gsLocal_1 = jsonObjs.toString();
            } catch (JSONException e) {
            }
            return gsLocal_1;
        } catch (IOException e) {
         }
        return "";
    }
//
//    public String FetchFlower(String gsLocal_1){
//        int g4Local_1 =  (int)(System.currentTimeMillis() / 1000);
////        mHaoChang.LoginFlag = "";
////        mHaoChang.gsUserID = "";
//        AUserInfo ownUserInfo = mHaoChang.GetUserFullInfo(0, gsLocal_1);
////MLOG(gsLocal_2);
//        if (ownUserInfo != null) {
//            int g4Local_2 = mHaoChang.GetUserID4Byte(ownUserInfo.UserIdS, gsFlowerTimeFile);
////g4Local_2=0;
//            if (g4Local_1 > g4Local_2){
//                mHaoChang.SwitchUser(gsLocal_1, ownUserInfo.longitude, ownUserInfo.latitude);
//                ResultNet resultNet = mHaoChang.WriteCommand(APPC_NEW_FLOWER, "", "");
////MLOG(resultNet.sucess);MLOG(resultNet.message);
//                if (resultNet.sucess){
//                    g4Local_1 = resultNet.message.indexOf('#');
//                    if (g4Local_1 > 0){
//                        gsLocal_1 = resultNet.message.substring(0, g4Local_1);
//                        g4Local_1 = MAPI.STRING_DEC32(gsLocal_1);
//                        mHaoChang.SetUserID4Byte(ownUserInfo.UserIdS, g4Local_1, gsFlowerTimeFile);
//                        g4Local_1 = resultNet.message.indexOf('@');
//                        if (g4Local_1 > 0) {
//                            String gsLocal_2 = resultNet.message.substring(g4Local_1+1, resultNet.message.length());
//                            mHaoChang.SetUserID4Byte(ownUserInfo.UserIdS, MAPI.STRING_DEC32(gsLocal_2), gsFlowerRecoderFile);
//                        }
//                    }
//                    return resultNet.message;
//                }
//            }
//        }
//        return "花出错：" +gsLocal_1;
//    }
    /*
    public static void UploadFileByTime(int video, String gsLocal_1){
        int g4Local_1 =  (int)(System.currentTimeMillis() / 1000);
        int g4Local_2 = gsLocal_1.indexOf('#');
        if (g4Local_2 < 1) {
            MLOG("帐号格式出错" + gsLocal_1);
            return;
        }
        mTransHaoChang.mHaoChang.mAuthorizeToken = "";
        mTransHaoChang.mHaoChang.mUserID = "";
        AUserInfo ownUserInfo = mTransHaoChang.mHaoChang.GetUserFullInfo(0, gsLocal_1);
        if (ownUserInfo == null) {
            MLOG("帐号信息出错" + gsLocal_1);
            return;
        }
        String gsLocal_2 = gsLocal_1.substring(g4Local_2 + 1, gsLocal_1.length());
        AUserInfo srcUserInfo = mTransHaoChang.mHaoChang.GetUserFullInfo(0, gsLocal_2);
        if (srcUserInfo == null || srcUserInfo.WorksQty == 0){
            MLOG("源帐号信息出错" + gsLocal_1);
            return;
        }
        mTransHaoChang.mHaoChang.SwitchUser(gsLocal_1, ownUserInfo.longitude, ownUserInfo.latitude);
        gsLocal_1 = String.format("%d：%s(%s)%d, 源：%s(%s)%s", mUploadCounter[0]/32, ownUserInfo.UserName, ownUserInfo.UserIdS, ownUserInfo.WorksQty, srcUserInfo.UserName, srcUserInfo.UserIdS, srcUserInfo.WorksQty);
        WriteToLogFile(gsLocal_1);

        g4Local_1 = 0;
        if (ownUserInfo.WorksQty == 0){
            MLOG("没有歌曲");
            g4Local_1 = 1;
        }
        else {
            ASongInfo ownSongInfo = mTransHaoChang.mHaoChang.GetSongInfo(ownUserInfo.IdS.get(0));
            if (ownSongInfo != null){
                if (IsDayPass(video==0 ? 3:1, ownSongInfo.CreateTime)){
                    g4Local_1 = 1;
                    MLOG("日期超过3天了");
                }
            }
        }
        if (g4Local_1 > 0){
            Random random = new Random();
            int g4Local_3 = 0;
            do {
                g4Local_2 = Math.abs(random.nextInt());
                g4Local_2 = 1 + (g4Local_2 % (srcUserInfo.WorksQty-1));
                gsLocal_1 = srcUserInfo.IdS.get(g4Local_2);
                g4Local_3 = 0;
                while (g4Local_3 < ownUserInfo.WorksQty) {
                    gsLocal_2 = ownUserInfo.IdS.get(g4Local_3);
                    if (gsLocal_2 == gsLocal_1){                   // 已经上传了
                        break;
                    }
                    ++g4Local_3;
                } ;
                if (g4Local_3 == ownUserInfo.WorksQty){
                    break;
                }
            } while (++g4Local_1 < 6);
//MLOG("准备上传：" + gsLocal_1 +" "+  g4Local_3 +" "+  ownUserInfo.WorksQty +" "+  g4Local_1 +" "+  g4Local_2);
            if (g4Local_1 < 6){
//MLOG(String.format("准备上传文件：%s(%d/%d)->%s %s", srcUserInfo.UserName, g4Local_2, srcUserInfo.WorksQty, ownUserInfo.UserName));
                gsLocal_1 = mTransHaoChang.copyMediaFile(video, g4Local_2, srcUserInfo);
//MLOG("上传文件完成：" + gsLocal_1);
                gsLocal_1 = String.format("上传文件：%s(%d/%d)->%s %s", srcUserInfo.UserName, g4Local_2, srcUserInfo.WorksQty, ownUserInfo.UserName, gsLocal_1);
                WriteToLogFile(gsLocal_1);
            }
        }
    }
    */
    public void returnBusyMessage(int step, int busy, String message){
        MSTRING(step + " " + busy + " " + message);
    }
    class TransToken {
        public int length = 0;
        public String transactionId;
        public String filename[];
        public String token[];
        public String key[];
        public String Authorization[];
    }
}

